package com.xiaoq.sites.endpoint;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;

import com.xiaoq.RedisTemplate4JedisCluster;
import com.xiaoq.coms.GenericDataResponse;
import com.xiaoq.coms.GenericListResponse;
import com.xiaoq.coms.GenericResponse;
import com.xiaoq.coms.util.ConstantUtil;
import com.xiaoq.sites.entity.SitesAnnouncementEntity;
import com.xiaoq.sites.entity.SitesEntity;
import com.xiaoq.sites.service.IAnnouncementService;
import com.xiaoq.sites.service.ISitesService;
import com.xiaoq.sites.vo.SitesAnnouncementVo;
import com.xiaoq.vo.PagingVo;


@RestController
@RequestMapping("/api/v1")
@Api(tags={"景区公告相关Api"})
public class SitesAnnouncementEndpoint {

	/*
	 * 查询景区公告（当前城市的景区） 按发布时间降序排列
	 */
	//@Reference(interfaceClass = IAnnouncementApi.class, check = false, version = "1.0.0")
	@Autowired
	private IAnnouncementService announcementApi;
	
	@Autowired
	private RedisTemplate4JedisCluster redisTemplate;
	
	@Autowired
	private ISitesService sitesSer;

	private static final class AnnouncementListResponse extends GenericListResponse<SitesAnnouncementVo> {
	};

	@ApiOperation(value = "景区公告列表", notes = "景区公告列表")
	@ApiResponses({ @ApiResponse(code = 200, response = AnnouncementListResponse.class, message = "公告列表") })
	@GetMapping(value = "/sites/sitesId/announcement", produces = { MediaType.APPLICATION_JSON_UTF8_VALUE })
	public ResponseEntity<?> sitesAnnouncement(
			@RequestParam(value = "sitesId", required = false) @ApiParam(value = "sitesId", required = false) String sitesId,
			@ModelAttribute(value="pvo")PagingVo pvo,HttpServletRequest request) {
		String errorMsg = "查询公告列表出错";
		String userId = (String) request.getAttribute("userId");

		if(sitesId==null){
			String shopId = redisTemplate.get(ConstantUtil.REDIS_PREFIX_MANAGER_SHOP_RELATION, userId);
			sitesId = shopId;
		}
		try {
			List<SitesAnnouncementEntity> sitesAnnounmentList = announcementApi.findAnnBySites(pvo, sitesId);
			List<SitesAnnouncementVo> sitesAnnouncementVoList = new ArrayList<>();
			SitesAnnouncementVo sitesAnnouncementVo = null;
			if (CollectionUtils.isNotEmpty(sitesAnnounmentList)) {
				for (SitesAnnouncementEntity sitesAnn : sitesAnnounmentList) {
					sitesAnnouncementVo = new SitesAnnouncementVo();
					BeanUtils.copyProperties(sitesAnn, sitesAnnouncementVo);
					sitesAnnouncementVoList.add(sitesAnnouncementVo);
				}
			}
			Long total = (Long) pvo.getTc();
			return AnnouncementListResponse.listAndCount(sitesAnnouncementVoList,total);
		} catch (Exception e) {
			e.printStackTrace();
			return GenericResponse.ng(errorMsg);
		}

	}

	@ApiOperation(value = "公告列表", notes = "查询公告列表")
	@ApiResponses({ @ApiResponse(code = 200, response = AnnouncementListResponse.class, message = "公告列表") })
	@GetMapping(value = "/sites/announcement", produces = { MediaType.APPLICATION_JSON_UTF8_VALUE })
	public ResponseEntity<?> list(
			@RequestParam(value = "city", required = false) @ApiParam(value = "城市编码", required = false) String city,
			@RequestParam(value = "district", required = false) @ApiParam(value = "区县编码（城市编码和区县编码传哪个就按哪个查如果两个都传就会两个都做检索条件）", required = false) String district,
			@ModelAttribute(value="pvo")PagingVo pvo) {

		String errorMsg = "查询公告列表出错";
		try {
			List<SitesAnnouncementEntity> sitesAnnounmentList = announcementApi.findAnnByCity(pvo, city,district);
			List<SitesAnnouncementVo> sitesAnnouncementVoList = new ArrayList<>();
			SitesAnnouncementVo sitesAnnouncementVo = null;
			if (CollectionUtils.isNotEmpty(sitesAnnounmentList)) {
				for (SitesAnnouncementEntity sitesAnn : sitesAnnounmentList) {
					sitesAnnouncementVo = new SitesAnnouncementVo();
					BeanUtils.copyProperties(sitesAnn, sitesAnnouncementVo);
					sitesAnnouncementVoList.add(sitesAnnouncementVo);
				}
			}
			Long total = (Long) pvo.getTc();

			return AnnouncementListResponse.listAndCount(sitesAnnouncementVoList,total);
		} catch (Exception e) {
			e.printStackTrace();
			return GenericResponse.ng(errorMsg);
		}

	}

	@ApiOperation(value = "公告发布", notes = "公告发布")
	@ApiResponses({ @ApiResponse(code = 200, response = GenericResponse.class, message = "公告发布") })
	@PostMapping(value = "/sites/announcement", produces = { MediaType.APPLICATION_JSON_UTF8_VALUE })
	public ResponseEntity<?> addSitesAnnouncement(
			 @RequestBody SitesAnnouncementVo sitesAnnouncementVo,HttpServletRequest request) {
		String userId = (String) request.getAttribute("userId");
		
		String shopId = redisTemplate.get(ConstantUtil.REDIS_PREFIX_MANAGER_SHOP_RELATION, userId);
		if(StringUtils.isEmpty(shopId)){
			return GenericResponse.ng("管理员没有对应商店");
		}
		SitesEntity sites = sitesSer.findById(shopId);
		if(sites==null){
			return GenericResponse.ng("没有对应的景区信息！");
		}
		String name = sites.getName();
		sitesAnnouncementVo.setSiteName(name);
		sitesAnnouncementVo.setSitesId(shopId);
		sitesAnnouncementVo.setPubStatus("1");
		sitesAnnouncementVo.setUserId(userId);
		sitesAnnouncementVo.setWarnLevel(5);
		sitesAnnouncementVo.setZoneProvince(sites.getZoneProvince());
		sitesAnnouncementVo.setZoneCity(sites.getZoneCity());
		sitesAnnouncementVo.setZoneDistrict(sites.getZoneDistrict());
		
		SitesAnnouncementEntity sitesAnnouncementEntity = new SitesAnnouncementEntity();
		BeanUtils.copyProperties(sitesAnnouncementVo, sitesAnnouncementEntity);
		boolean result = announcementApi.addSitesAnnounment(sitesAnnouncementEntity);
		if (result) {
			return GenericResponse.ok();
		}
		return GenericResponse.ng("公告发布失败");
	}

	@ApiOperation(value = "政府公告发布", notes = "政府公告发布")
	@ApiResponses({ @ApiResponse(code = 200, response = GenericResponse.class, message = "政府公告发布") })
	@PostMapping(value = "/sites/announcement_gov", produces = { MediaType.APPLICATION_JSON_UTF8_VALUE })
	public ResponseEntity<?> addGovAnnouncement(
			@RequestBody SitesAnnouncementVo sitesAnnouncementVo,HttpServletRequest request) {
		String userId = (String) request.getAttribute("userId");



		sitesAnnouncementVo.setPubStatus("1");
		sitesAnnouncementVo.setUserId(userId);
		sitesAnnouncementVo.setWarnLevel(5);

		SitesAnnouncementEntity sitesAnnouncementEntity = new SitesAnnouncementEntity();
		BeanUtils.copyProperties(sitesAnnouncementVo, sitesAnnouncementEntity);
		boolean result = announcementApi.addSitesAnnounment(sitesAnnouncementEntity);
		if (result) {
			return GenericResponse.ok();
		}
		return GenericResponse.ng("公告发布失败");
	}

	@ApiOperation(value = "删除公告", notes = "公告删除")
	@ApiResponses({ @ApiResponse(code = 200, response = GenericResponse.class, message = "公告删除") })
	@DeleteMapping(value = "/sites/{id}/announcement", produces = { MediaType.APPLICATION_JSON_UTF8_VALUE })
	public ResponseEntity<?> delSitesAnnouncement(
			@PathVariable(value = "id", required = true) @ApiParam(value = "id", required = true) String id) {
		boolean result = announcementApi.delSitesAnnounment(id);
		if (result) {
			return GenericResponse.ok();
		}
		return GenericResponse.ng("删除失败");
	}

	/*
	 * @ApiOperation(value="使公告失效", notes="使公告失效")
	 * 
	 * @ApiResponses({
	 * 
	 * @ApiResponse(code = 200, response = String.class, message = "使公告失效") })
	 * 
	 * @PutMapping(value="/announcement",
	 * produces={MediaType.APPLICATION_JSON_UTF8_VALUE}) public String
	 * invalSitesAnnouncement(@RequestParam(value="annId",required=true)
	 * @ApiParam(value="annId",required=true)String annId){ boolean result =
	 * announcementApi.invalSitesAnnounment(annId); if(result){ return
	 * "是公告失效成功"; } return "是公告失效失败"; }
	 */
	@ApiOperation(value = "编辑公告", notes = "重新编辑公告")
	@ApiResponses({ @ApiResponse(code = 200, response = GenericResponse.class, message = "公告编辑") })
	@PutMapping(value = "/sites/{id}/announcement", produces = { MediaType.APPLICATION_JSON_UTF8_VALUE })
	public ResponseEntity<?> editeSitesAnnouncement(
			@RequestBody SitesAnnouncementVo sitesAnnouncementVo) {
		SitesAnnouncementEntity sitesAnno = new SitesAnnouncementEntity();
		BeanUtils.copyProperties(sitesAnnouncementVo, sitesAnno);
		boolean result = announcementApi.editSitesAnnounment(sitesAnno);
		if (result) {
			return GenericResponse.ok();
		}
		return GenericResponse.ng("公告编辑失败");
	}
	
	@ApiOperation(value = "景区公告详情", notes = "景区公告详情")
	@ApiResponses({ @ApiResponse(code = 200, response = AnnouncementListResponse.class, message = "景区公告详情") })
	@GetMapping(value = "/details", produces = { MediaType.APPLICATION_JSON_UTF8_VALUE })
	public ResponseEntity<?> sitesAnnouncementDetail(
			@RequestParam(value = "announceId", required = false) @ApiParam(value = "announceId", required = false) String announceId) {
		SitesAnnouncementEntity sitesAnnounce = announcementApi.getSitesAnnounce(announceId);
		SitesAnnouncementVo sitesAnnounceVo = new SitesAnnouncementVo();
		BeanUtils.copyProperties(sitesAnnounce, sitesAnnounceVo);
		
		return GenericDataResponse.okWithData(sitesAnnounceVo);
		

	}
}
